﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
///User 的摘要说明
/// </summary>
public  class User:Component
{

    protected string name;
    protected string sex;
    protected string idcard;
    protected string birthday;

    public string Birthday
    {
        get { return birthday; }
        set { birthday = value; }
    }


    public string Idcard
    {
        get { return idcard; }
        set { idcard = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public string Sex
    {
        get { return sex; }
        set { sex = value; }
    }

	public User()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public User(
        string idcard,
        string name,
        string sex,
        string birthday)
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        this.name = name;
        this.sex = sex;
        this.idcard = idcard;
        this.birthday = birthday;
    }
    public override string insert(SqlConnection sqlcon)
    {
       try
       {
           string sqlCmdStr = "insert UserTable(IDcard,name,sex,birthday) values("
          + SqlFormat.ap(idcard) + ","
          + SqlFormat.ap(name) + ","
          + sex + ","
          + SqlFormat.ap(birthday)
          + ");";

           SqlCommand sqlCmd = new SqlCommand(sqlCmdStr, sqlcon);

           if (sqlCmd.ExecuteNonQuery() != 1)
           {
               return null;
           }

           return "ok";
       }
       catch (System.Exception ex)
       {
           return ex.Source;
       }
      
    }

    public int compareToData(SqlConnection sqlcon)
    {
        try
        {
            string sqlcmdstr = "SELECT * From UserTable WHERE IDCard=" + SqlFormat.ap(idcard);

            SqlDataAdapter sqldap = new SqlDataAdapter();

            sqldap.SelectCommand = new SqlCommand(sqlcmdstr, sqlcon);

            DataTable datatd = new DataTable();

            sqldap.Fill(datatd);

            if (datatd.Rows.Count == 0)
            {
                return -1;
            }
            else
            {
                string mName = datatd.Rows[0].ItemArray[1].ToString();
                string mSex = datatd.Rows[0].ItemArray[2].ToString();
                string mbirthday = datatd.Rows[0].ItemArray[3].ToString();

                DateTime datetime1, datetime2;

                if (DateTime.TryParse(this.birthday, out datetime1) && DateTime.TryParse(mbirthday, out datetime2))
                {
                    if (mName.Equals(name) && mSex.Equals(sex) && datetime1.CompareTo(datetime2) == 0)
                    {
                        return 0;
                    }

                }
                else
                {
                    return 1;
                }


            }



            return 1;
        }
        catch (System.Exception ex)
        {
            return -2;
        }
        
    }
}